<template>
  <div class="Statistics">
    <el-alert
      title="日志消息只保留最近15天"
      type="success">
    </el-alert>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="登录日志" name="first">
        <el-table
          :data="loginTableDate"
          style="width: 100%"
          :default-sort="{prop: 'date', order: 'descending'}"
        >
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column
            prop="operationTime"
            label="时间"
            sortable
            :formatter="formatter"
            width="180">
          </el-table-column>
          <el-table-column
            prop="ipaddr"
            label="IP"
            sortable
            width="150">
          </el-table-column>
          <el-table-column
            prop="operationType"
            label="状态"
            sortable
            width="120">
          </el-table-column>
          <el-table-column
            prop="city"
            label="地址"
          >
          </el-table-column>
          <el-table-column
            prop="operationUsername"
            label="用户名"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="deleteloginlog(scope)"
                type="danger"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          style="float: right;margin-top: 20px;margin-right: 15px"
          :hide-on-single-page="true"
          layout="prev, pager, next"
          :current-page="2"
          @current-change="handleCurrentChange"
          :total="loginTableDate.length">
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane label="操作日志" name="second">
        <el-table
          :data="SysLogTabelData"
          style="width: 100%"
          :default-sort="{prop: 'date', order: 'descending'}"
        >
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column
            prop="operationTime"
            label="日期"
            sortable
            :formatter="formatter"
            width="180">
          </el-table-column>
          <el-table-column
            prop="operationUsername"
            label="用户"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="operationType"
            label="状态"
            sortable
            width="120">
          </el-table-column>
          <el-table-column
            prop="operationName"
            label="操作类型"
          >
          </el-table-column>
          <el-table-column
            prop="address"
            label="IP"
            :formatter="formatter">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="deleteSyslog(scope)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>


      <el-tab-pane label="学生管理" name="third" v-if="this.$cookie.get('role')>=5000">
        <el-row :gutter="20">
          <el-col :xs="24" :md="8">
            <el-select v-model="college" placeholder="请选择学院" style="width: 95%;margin-top: 5px" @change="getmajor">
              <el-option
                v-for="item in collages"
                :key="item.colId"
                :label="item.colname"
                :value="item.colId">
              </el-option>
            </el-select>
          </el-col>
          <el-col :xs="24" :md="8">
            <el-select v-model="major" placeholder="请选择专业" style="width: 95%;margin-top: 5px" @change="getclazz">
              <el-option
                v-for="item in majors"
                :key="item.id"
                :label="item.majorname"
                :value="item.id">
              </el-option>
            </el-select>

          </el-col>

          <el-col :xs="24" :md="8">
            <el-select v-model="clazzId" placeholder="请选择班级" style="width: 95%;margin-top: 5px" @change="getstudent">
              <el-option
                v-for="item in calzzs"
                :key="item.clazzId"
                :label="item.clazzName"
                :value="item.clazzId">
              </el-option>
            </el-select>

          </el-col>
        </el-row>
        <el-table
          :data="studentlist"
          style="width: 100%"
          :default-sort="{prop: 'date', order: 'descending'}"
        >
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="studentId"
            label="学号"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="sex"
            label="性别"
            sortable
            width="90">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话"
            sortable
            width="220">
          </el-table-column>
          <el-table-column
            prop="state"
            label="状态"
            sortable
            :formatter="formatterstate"
            width="90">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="warning"
                @click="reset_password(scope)"
              >重置密码
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="deleteUser(scope)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="教师管理" name="fourth" v-if="this.$cookie.get('role')>=5000">

        <el-table
          :data="studentlist"
          style="width: 100%"
          :default-sort="{prop: 'date', order: 'descending'}"
        >
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="studentId"
            label="学号"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="sex"
            label="性别"
            sortable
            width="90">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话"
            sortable
            width="220">
          </el-table-column>
          <el-table-column
            prop="state"
            label="状态"
            sortable
            :formatter="formatterstate"
            width="90">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="warning"
                @click="reset_password(scope)"
              >重置密码
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="deleteUser(scope)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
  export default {
    name: "Statistics",
    data() {
      return {
        activeName: 'first',
        loginTableDate: [],
        SysLogTabelData: [],
        studentlist: [],
        college: "",
        collages: "",
        major: "",
        majors: "",
        clazzId: "",
        calzzs: "",


      }
    },
    methods: {
      formatterstate(row, column) {

        var fmt = "";
        if (row.state == "1") {
          fmt = "正常"
        } else if (row.state == "2") {
          fmt = "毕业"
        } else if (row.state == "3") {
          fmt = "休学"
        } else {
          fmt = "异常"
        }
        return fmt;
      },
      getmajor(va) {
        var self = this;
        this.major = '';
        this.clazzId = '';
        this.$http.get(this.URL.api + "querybycolid?colid=" + va).then(function (response) {

          self.majors = response.data.data;

        }).catch(function (response) {

        });
      },
      getstudent(va) {
        var self = this;
        this.$http.get(this.URL.api + "user/" + va + "/queryByStuClazz").then(function (response) {

          self.studentlist = response.data.data;

        }).catch(function (response) {

        });
      },
      getclazz(va) {

        var self = this;

        this.clazzId = '';
        this.$http.get(this.URL.api + "querybymajorid?majorid=" + va).then(function (response) {

          self.calzzs = response.data.data;

        }).catch(function (response) {

        });
      },
      reset_password(data) {
        console.log(data.row.uid)
        this.$http.get(this.URL.api + "user/" + data.row.uid + "/reset_password", res => {
        })

      }, deleteUser(data) {
        console.log(data.row.uid)
        this.$http.get(this.URL.api + "user/" + data.row.uid + "/deleteUser", res => {
        })

      },
      formatter(row, column) {
        var fmt = this.UserInfo.dateFormat(row.operationTime);
        return fmt;
      },

      deleteloginlog(row) {
        console.log(row.$index)
        var self = this;
        this.$http.delete(this.URL.api + "log/deleteOneByLoginid/" + row.row.logId).then(res => {
          if (res.data.flag) {
            self.loginTableDate.splice(row.$index, 1);
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败");
          }

        })

      } ,
      handleCurrentChange() {

      },
      deleteSyslog(row) {
        console.log(row.$index)
        var self = this;
        this.$http.delete(this.URL.api + "log/deleteOneBySysLogid/" + row.row.logId).then(res => {
          if (res.data.flag) {
            self.SysLogTabelData.splice(row.$index, 1);
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败");
          }

        })

      }




      , handleClick() {

      }
    },
    created() {
      var self = this;
      this.$http.get(this.URL.api + "log/queryByUsernameLogin/" + this.$cookie.get("userid")).then(res => {

        if (res.data.flag) {

          self.loginTableDate = res.data.data

        }

      })
      this.$http.get(this.URL.api + "log/queryByUsernameSys/" + this.$cookie.get("userid")).then(res => {

        if (res.data.flag) {

          self.SysLogTabelData = res.data.data

        }

      });
      // 获取学院
   if(this.$cookie.get('role')>=5000)
      this.$http.get(this.URL.api + "queryallcollage").then(function (response) {
        self.collages = response.data.data;
      }).catch(function (response) {
      });


    },


  }
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    width: 480px;
  }
</style>
